//Rectangle.cpp
#ifndef RECTANGLE_H_
#define RECTANGLE_H_
#include <iostream>
#include <cmath>
#include "Point.h"
using namespace std;

class Rectangle {
private:
    Point point1;
    Point point2;
public:
    Rectangle(Point p1 = Point(0, 0), Point p2 = Point(0, 0));
    Rectangle(double x1, double y1, double x2, double y2);
    Rectangle(const Rectangle& rectangle);
    ~Rectangle();
    void Set(Point p1, Point p2);
    void Set(double x1, double y1, double x2, double y2);
    void Print();
    Point centerPoint();
    double getPerimeter();
    double getArea();
};

Rectangle::Rectangle(Point p1, Point p2) : point1(p1), point2(p2) {}

Rectangle::Rectangle(double x1, double y1, double x2, double y2) : point1(x1, y1), point2(x2, y2) {}

Rectangle::Rectangle(const Rectangle& rectangle) {
    this->point1 = rectangle.point1;
    this->point2 = rectangle.point2;
}

Rectangle::~Rectangle() {
    if (&point1 != nullptr) {
        delete &point1;
    }
    if (&point2 != nullptr) {
        delete &point2;
    }
}

void Rectangle::Set(Point p1, Point p2) {
    this->point1 = p1;
    this->point2 = p2;
}

void Rectangle::Set(double x1, double y1, double x2, double y2) {
    this->point1.setX(x1);
    this->point1.setY(y1);
    this->point2.setX(x2);
    this->point2.setY(y2);
}

void Rectangle::Print() {
    cout << "两端点坐标为：";
    point1.Print();
    cout << ", ";
    point2.Print();
    cout << "\n中心点为：";
    centerPoint().Print();
    cout << "\n周长为：" << getPerimeter() <<
    "\n" << "面积为：" << getArea() << endl;
}

Point Rectangle::centerPoint() {
    Point* p = new Point();
    p->setX((point1.getX() + point2.getX()) / 2);
    p->setY((point1.getY() + point2.getY()) / 2);
    return *p;
}

double Rectangle::getPerimeter() {
    double length = abs(point1.getX() - point2.getX());
    double width = abs(point1.getY() - point2.getY());
    return (length + width) * 2;
}

double Rectangle::getArea() {
    double length = abs(point1.getX() - point2.getX());
    double width = abs(point1.getY() - point2.getY());
    return length *  width;
}
#endif